package com.ming.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ming.constant.CategoryConstant;
import com.ming.domain.Category;
import com.ming.mapper.CategoryMapper;
import com.ming.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * @Author:何元才
 * @Desc: CacheConfig(cacheNames = " com.ming.service.impl.CategoryServiceImpl ")
 * 命名空间的隔离
 */
@Service
@Slf4j
@CacheConfig(cacheNames = "com.ming.service.impl.CategoryServiceImpl")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 全查询商品分类
     * 缓存
     * 1.代码形式缓存
     * 2.注解的方式缓存
     * Cacheable 使用aop的方式 目标方法执行后 将返回值作为value存入 redis
     * 后台查询不需要做状态的判断
     * 前端给用户看需要一定的判断
     *
     * @return
     */
    @Override
    @Cacheable(key = CategoryConstant.CATEGORY_ALL_PREFIX)
    public List<Category> list() {
        List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .orderByDesc(Category::getUpdateTime)
        );
        return categoryList;
    }

    /**
     * 查询商品父级分类
     * 也需要缓存
     *
     * @return
     */
    @Override
    @Cacheable(key = CategoryConstant.CATEGORY_PARENT_PREFIX)
    public List<Category> findCategoryParent() {

        List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getCategoryId, 0L)
                .eq(Category::getStatus, 1)
        );
        return categoryList;
    }

    @Override
    @Caching(evict = {
            @CacheEvict(key = CategoryConstant.CATEGORY_PARENT_PREFIX),
            @CacheEvict(key = CategoryConstant.CATEGORY_ALL_PREFIX)
    })
    public boolean save(Category category) {
        log.info("新增商品分类{}", JSON.toJSONString(category));
        category.setRecTime(new Date());
        category.setUpdateTime(new Date());
        category.setShopId(1L);
        //新增商品分类 1.新增父节点 2 新增字节点
        Long parentId = category.getParentId();
        if(parentId == null || parentId.equals(0L)){
            //说明父级类
            parentId = 0L;
            category.setGrade(1);
        }else  {
            //说明子分类 需要查询一下他的父节点
            Category parent = categoryMapper.selectById(parentId);
            if (ObjectUtils.isEmpty(parent)){
                throw  new IllegalArgumentException("新增商品分类时父节点不存在");
            }
            category.setGrade(2);
        }
        return super.save(category);
    }
}
